package com.idanchuang.component.consumer.ribbon.lb;

import com.netflix.client.config.IClientConfig;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext;
import org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryPolicy;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerContext;

/**
 * @author yjy
 * Created at 2021/6/18 2:25 下午
 */
public class AcgRibbonLoadBalancedRetryPolicy extends RibbonLoadBalancedRetryPolicy {

    public AcgRibbonLoadBalancedRetryPolicy(String serviceId, RibbonLoadBalancerContext context, ServiceInstanceChooser loadBalanceChooser) {
        super(serviceId, context, loadBalanceChooser);
    }

    public AcgRibbonLoadBalancedRetryPolicy(String serviceId, RibbonLoadBalancerContext context, ServiceInstanceChooser loadBalanceChooser, IClientConfig clientConfig) {
        super(serviceId, context, loadBalanceChooser, clientConfig);
    }

    @Override
    public boolean canRetry(LoadBalancedRetryContext context) {
        Throwable throwable = context.getLastThrowable();
        if (throwable != null) {
            if (throwable instanceof org.apache.http.conn.ConnectTimeoutException) {
                return true;
            }
            if (throwable instanceof org.apache.http.conn.HttpHostConnectException) {
                return true;
            }
        }
        return super.canRetry(context);
    }

}
